home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / xascii / xascii.c < prev    next >
C/C++ Source or Header  |  1995-06-22  |  22KB  |  581 lines

  1. /*****************************************************************************
  2. * XAscii                                                      by Ken Kirksey *
  3. *                                                                            *
  4. * XAscii displays a table of ascii characters and their respective values.   *
  5. * The user choses which format the values are displayed in:  either hex,     *
  6. * decimal or octal.                                                          *
  7. *                                                                            *
  8. * BASICALLY THE WAY IT WORKS                                                 *
  9. *        In xascii.h four static arrays of strings are declared, one for      *
  10. *       ascii characters, one for decimal values, one for hex values and one *
  11. *        for octal values.  Each of these are broken up into five separate    *
  12. *        strings, each of which make up a column (label widget) in the xascii *
  13. *        window.  The ascii value columns remain static, they never change.   *
  14. *        The label widgets that hold the values, however, change whenever the *
  15. *        user changes the notational mode (clicks on the dec ,hex, or octal   *
  16. *       button).  What happens when one of these buttons is clicked is that  *
  17. *       an XtVaSetValues is done on the value label widgets to change their  *
  18. *        label resource to display the values in the notation specified by the*
  19. *        button press.                                                        *
  20. *                                                                            *
  21. * Send all kudos, complaints, suggestions, or bug reports to:                *
  22. *        Ken Kirksey  (kkirksey@eng.auburn.edu)                               *
  23. *                                                                            *
  24. * BTW:  I use 4 column tabs for coding, so if your text editor isn't set for *
  25. *         4 column tabs, this program probably looks really funny.             *
  26. *                                                                               *
  27. ******************************************************************************
  28. * Version 1.1                                                       5 Aug 91 *
  29. *                                                                            *
  30. *  Removed the #include <strings.h> as this was a portability no-no.         *
  31. *  Added an Imakefile (courtesy of Dave Elliot) to the release archive.      *   *                                                                               *
  32. ******************************************************************************
  33. * Version 1.2                                                       6 Aug 91 *
  34. *                                                                            *
  35. * Fixed the problem that was causing my use of the valueFont resource to     *
  36. * kill the program.  The bug was pointed out by Dave Brooks.                 *
  37. *                                                                               *
  38. *****************************************************************************/                                                                           
  39.  
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <X11/Intrinsic.h>
  43. #include <X11/Shell.h>
  44. #include <X11/Xos.h>
  45. #include <X11/StringDefs.h>
  46. #include <X11/Xaw/Command.h>
  47. #include <X11/Xaw/Toggle.h>
  48. #include <X11/Xaw/Label.h>    
  49. #include <X11/Xaw/Form.h>
  50. #include "xascii.h"
  51.  
  52. #define ROWS 16
  53. #define COLS  8  
  54.  
  55. #define MAJOR_VERT_DIST 15      /* between gps */
  56. #define MINOR_VERT_DIST 10      /* within gps */
  57.  
  58. #define MAJOR_HORIZ_DIST 10     /* between grps */
  59. #define MINOR_HORIZ_DIST 30     /* within or to align gps */
  60.  
  61. /*===========================================================================+
  62. | Function Declarations                                                      |
  63. +===========================================================================*/
  64. static void Quit(),                    /* Callbacks                            */
  65.             ToHex(),
  66.             ToDec(),
  67.             ToOct(),
  68.             Done(),
  69.  
  70.             AboutXascii(),            /* Actions                                */
  71.  
  72.             Syntax();                /* Utilitie                                */
  73.  
  74. static char *ascii_val();
  75.  
  76. /*===========================================================================+
  77. | Global Variable Declarations                                               |
  78. +===========================================================================*/
  79.  
  80. static XtAppContext appl_context;
  81.  
  82. static XtActionsRec about_box[]= {
  83.         {"about_xascii", AboutXascii},
  84.         {"ToDec", ToDec},
  85.         {"ToHex", ToHex},
  86.         {"ToOct", ToOct},
  87.         {"Quit",  Quit},
  88. };
  89.  
  90. static Widget     toplevel,
  91.                 base_win,
  92.                 title_label,
  93.                 quit_button,
  94.                 toggles[3],
  95.                 ascii_list[COLS],
  96.                 value_list[COLS],
  97.                 nm_list[COLS],
  98.                 pshell,
  99.                 about_base_win,
  100.                 about_label,
  101.                 done_button;
  102.  
  103. char        **hex_columns,
  104.             **decimal_columns,
  105.             **octal_columns,
  106.             **nm_columns,
  107.             **ascii_columns;
  108.  
  109. char *keytranstbl="#override\n\
  110.     <Key>q: Quit()\n\
  111.     <Key>d: ToDec()\n\
  112.     <Key>h: ToHex()\n\
  113.     <Key>o: ToOct()\n\
  114. ";
  115.  
  116. /*===========================================================================+
  117. | MAIN                                                                       | +===========================================================================*/
  118. main (argc,argv)
  119.     int     argc; 
  120.     char     ** argv;
  121. {
  122.     int        i, j;
  123.     char buf[100], buf1[10];
  124.     XtTranslations keytrans;
  125.     
  126. /*---------------------------------------------------------------------------+
  127. | Break up the Ascii table and values into separate columns
  128. | (ROWS rows, COLS cols) for display.
  129. +---------------------------------------------------------------------------*/
  130.     ascii_columns   = (char **)XtCalloc (COLS, sizeof (char *));
  131.     hex_columns     = (char **) XtCalloc (COLS, sizeof (char *));
  132.     decimal_columns = (char **) XtCalloc (COLS, sizeof (char *));
  133.     octal_columns   = (char **) XtCalloc (COLS, sizeof (char *));
  134.     nm_columns      = (char **) XtCalloc (COLS, sizeof (char *));
  135.  
  136.     for (i=0; i<COLS; i++)
  137.     {
  138.         ascii_columns[i]    = (char *)  XtCalloc (90, sizeof (char));
  139.         hex_columns[i]      = (char *)  XtCalloc (90, sizeof (char));
  140.         decimal_columns[i]  = (char *)  XtCalloc (90, sizeof (char));
  141.         octal_columns[i]    = (char *)  XtCalloc (90, sizeof (char));
  142.         nm_columns[i]       = (char *)  XtCalloc (90, sizeof (char));
  143.     
  144.         for (j= (i*ROWS); j < ((i+1) * ROWS); j++)
  145.         {
  146.             sprintf( buf, "%3.3s\n", ascii_val(j, buf1) );
  147.             strcat (ascii_columns[i], buf);
  148.  
  149.             sprintf( buf, "%s\n", name_values[j]);
  150.             strcat (nm_columns[i], buf );
  151.  
  152.             sprintf(buf, "%3x\n", j ) ;
  153.             strcat (hex_columns[i], buf);
  154.  
  155.             sprintf(buf, "%3d\n", j ) ;
  156.             strcat (decimal_columns[i], buf);
  157.  
  158.             sprintf(buf, "%03o\n", j ) ;
  159.             strcat (octal_columns[i], buf);
  160.         }
  161.     }
  162.  
  163.     /*-----------------------------------------------------------------------+
  164.     | Begin Widget Initialization.                                           |
  165.     +-----------------------------------------------------------------------*/     
  166.     toplevel = XtAppInitialize(
  167.                     &appl_context,
  168.                     "XAscii",
  169.                     options,
  170.                     XtNumber(options),
  171.                     &argc,argv,NULL, NULL,0);
  172.  
  173.     /*-----------------------------------------------------------------------+
  174.     | Check for Invalid command line options                                 |
  175.     +-----------------------------------------------------------------------*/
  176.     if (argc > 1)
  177.         Syntax (argc, argv);
  178.  
  179.     
  180.     keytrans = XtParseTranslationTable( keytranstbl );
  181.     
  182.     XtVaGetApplicationResources (toplevel,
  183.                     &app_data,
  184.                     resources,
  185.                     XtNumber (resources),
  186.                     NULL);
  187.  
  188.     base_win = XtVaCreateManagedWidget(
  189.                     "base_win", 
  190.                     formWidgetClass,
  191.                     toplevel,
  192.                     XtNtranslations, keytrans,
  193.                     NULL, 0);
  194.  
  195.     /*----------------------------------------------------------------------+
  196.     |  Note the translation that invokes that about box pop up on a button  |
  197.     |  click in this label widget.                                          |
  198.     +----------------------------------------------------------------------*/
  199.     title_label    = XtVaCreateManagedWidget (
  200.                     "title_label",
  201.                     labelWidgetClass,
  202.                     base_win,
  203.                         XtNforeground, app_data.highlight_color,
  204.                         XtNlabel, "X ASCII Chart",
  205.                         XtNwidth, 408,
  206.                         XtNtranslations,
  207.                             XtParseTranslationTable(defaultTranslations),
  208.                          XtNhorizDistance, 200,
  209.                         XtVaTypedArg,
  210.                             XtNbackground,
  211.                             XtRString,
  212.                             white_bg,
  213.                             sizeof (white_bg),
  214.                         XtVaTypedArg,
  215.                             XtNfont,
  216.                             XtRString,
  217.                             title_font,
  218.                             sizeof (title_font),
  219.                     NULL, 0);
  220.  
  221.     /*-----------------------------------------------------------------------+
  222.     |Create the label widgets for chart display. Default value mode is dec.  |
  223.     +-----------------------------------------------------------------------*/
  224.     for (i=0; i<COLS;  i++)
  225.     {
  226.         value_list[i] = XtVaCreateManagedWidget(
  227.                         "value_list",
  228.                         labelWidgetClass,
  229.                         base_win,
  230.                             XtNforeground, app_data.highlight_color,
  231.                             XtNfont, app_data.value_font,  
  232.                             XtNfromVert, title_label, 
  233.                             XtNlabel, decimal_columns[i],
  234.                             XtNhorizDistance, MAJOR_HORIZ_DIST,
  235.                             XtNborderWidth, 0,
  236.                             XtNvertDistance, MAJOR_VERT_DIST,
  237.                             XtVaTypedArg,
  238.                                 XtNbackground,
  239.                                 XtRString,
  240.                                 white_bg,
  241.                                 sizeof (white_bg),
  242.  
  243.                         NULL);
  244.  
  245.         ascii_list[i] = XtVaCreateManagedWidget(
  246.                         "ascii_list",
  247.                         labelWidgetClass,
  248.                         base_win,
  249.                             XtNfont, app_data.value_font,  
  250.                             XtNfromVert, title_label, 
  251.                             XtNlabel, ascii_columns[i],
  252.                             XtNfromHoriz, value_list[i],
  253.                             XtNborderWidth, 0,
  254.                             XtNvertDistance, MAJOR_VERT_DIST,
  255.                             XtNhorizDistance, 0,
  256.                             XtVaTypedArg,
  257.                                 XtNbackground,
  258.                                 XtRString,
  259.                                 white_bg,
  260.                                 sizeof (white_bg),
  261.  
  262.                         NULL);
  263.  
  264.         nm_list[i] = XtVaCreateManagedWidget(
  265.                         "name_list",
  266.                         labelWidgetClass,
  267.                         base_win,
  268.                             XtNfont, app_data.value_font,  
  269.                             XtNfromVert, title_label,
  270.                             XtNfromHoriz, ascii_list[i],
  271.                             XtNlabel, nm_columns[i],
  272.                             XtNborderWidth, 0,
  273.                             XtNvertDistance, MAJOR_VERT_DIST,
  274.                             XtNhorizDistance, 0,
  275.                             XtVaTypedArg,
  276.                                 XtNbackground,
  277.                                 XtRString,
  278.                                 white_bg,
  279.                                 sizeof (white_bg),
  280.  
  281.                         NULL);
  282.  
  283.  
  284.     }
  285.         
  286.     for (i=1; i<COLS; i++)
  287.         XtVaSetValues (value_list[i], XtNfromHoriz, nm_list[i-1], NULL);
  288.     /*-----------------------------------------------------------------------+
  289.     | Create the buttons for value mode selection.  Make them a radio group, |
  290.     | so that only one will be selected (highlighted) at a time.             |
  291.     +-----------------------------------------------------------------------*/
  292.     for (i=0; i<3; i++)
  293.     {
  294.     char name[40];
  295.  
  296.         sprintf(name,"toggle%d", i);
  297.         toggles[i] = XtVaCreateManagedWidget (
  298.                     name,
  299.                     toggleWidgetClass, 
  300.                     base_win,
  301.                         XtNforeground, app_data.highlight_color,
  302.                         XtNborderColor, app_data.highlight_color,
  303.                         XtNfromVert, ascii_list[0],
  304.                         XtNvertDistance, MAJOR_VERT_DIST,
  305.                         XtNwidth, 95,
  306.                         XtNresize, FALSE,
  307.                         XtVaTypedArg,
  308.                             XtNbackground,
  309.                             XtRString,
  310.                             white_bg,
  311.                             sizeof (white_bg),
  312.  
  313.                     NULL, 0);
  314.  
  315.         if (i != 0)
  316.             XtVaSetValues(toggles[i], XtNfromHoriz, toggles[i-1], NULL);
  317.  
  318.         XtVaSetValues (toggles[i], XtNradioGroup, toggles[0], NULL);
  319.     }
  320.  
  321.     XtVaSetValues (toggles[0],
  322.                     XtNlabel,           "Hex",
  323.                     XtNhorizDistance,   MAJOR_HORIZ_DIST,
  324.                     XtNfromHoriz,       nm_list[1],
  325.                     NULL);
  326.  
  327.     XtVaSetValues (toggles[1],
  328.                     XtNstate, TRUE,
  329.                     XtNlabel, "Decimal", 
  330.                     XtNhorizDistance, MINOR_HORIZ_DIST,
  331.                     NULL);
  332.  
  333.     XtVaSetValues (toggles[2],
  334.                     XtNlabel,    "Octal",
  335.                     XtNhorizDistance, MINOR_HORIZ_DIST,
  336.                     NULL);
  337.  
  338.     XtAddCallback (toggles[0], XtNcallback, ToHex, NULL); 
  339.     XtAddCallback (toggles[1], XtNcallback, ToDec, NULL);
  340.     XtAddCallback (toggles[2], XtNcallback, ToOct, NULL); 
  341.  
  342.     
  343.     quit_button = XtVaCreateManagedWidget (
  344.                     "quit_button", 
  345.                     commandWidgetClass, 
  346.                     base_win,
  347.                         XtNfromVert, toggles[0],
  348.                         XtNfromHoriz, toggles[0],
  349.                         XtNlabel, "Quit",
  350.                         XtNwidth, 100,
  351.                         XtNvertDistance, MAJOR_VERT_DIST,
  352.                         XtNhorizDistance, MINOR_HORIZ_DIST,
  353.                         XtVaTypedArg,
  354.                             XtNbackground,
  355.                             XtRString,
  356.                             white_bg,
  357.                             sizeof (white_bg),
  358.  
  359.                     NULL, 0);
  360.  
  361.     XtAddCallback (quit_button, XtNcallback, Quit, NULL);
  362.  
  363.     /*-----------------------------------------------------------------------+
  364.     | Create the About Box.  Invoked on click in title label.                |
  365.     +-----------------------------------------------------------------------*/
  366.     pshell = XtVaCreatePopupShell (
  367.                     "pshell",
  368.                     transientShellWidgetClass,
  369.                     toplevel,
  370.                     NULL);
  371.  
  372.     about_base_win = XtVaCreateManagedWidget(
  373.                     "about_base_win",
  374.                     formWidgetClass,
  375.                     pshell,
  376.  
  377.                     NULL);
  378.  
  379.     about_label = XtVaCreateManagedWidget(
  380.                     "about_label",
  381.                     labelWidgetClass,
  382.                     about_base_win,
  383.                         XtVaTypedArg,
  384.                             XtNbackground,
  385.                             XtRString,
  386.                             white_bg,
  387.                             sizeof (white_bg),
  388.                         XtNlabel, about_text,
  389.                         XtNforeground, app_data.highlight_color,
  390.                         XtNborderColor, app_data.highlight_color,
  391.                         XtNborderWidth, 4,
  392.                     NULL);
  393.  
  394.     done_button = XtVaCreateManagedWidget(
  395.                     "done_button",
  396.                     commandWidgetClass,
  397.                     about_base_win,
  398.                         XtVaTypedArg,
  399.                             XtNbackground,
  400.                             XtRString,
  401.                             white_bg,
  402.                             sizeof (white_bg),
  403.                         XtNfromVert, about_label,
  404.                         XtNvertDistance, 10,
  405.                         XtNhorizDistance, 165,
  406.                         XtNlabel, "Slainte!",
  407.                     NULL);
  408.  
  409.     XtAddCallback (done_button, XtNcallback, Done, NULL);
  410.  
  411.     XtAppAddActions (appl_context, about_box, XtNumber(about_box) );
  412.  
  413.      XtRealizeWidget(toplevel);
  414.      XtAppMainLoop(appl_context);
  415. }
  416.  
  417. /*===========================================================================+
  418. | CALLBACK FUNCTIONS                                                          |
  419.  +===========================================================================*/
  420.  
  421. /*---------------------------------------------------------------------------+
  422. | Quit                                                                       |
  423. |         Quit the application.  Invoked by click on quit button.            |
  424. +---------------------------------------------------------------------------*/
  425. static void Quit (w,ignore1,ignore2)
  426. Widget w; XtPointer ignore1, ignore2;
  427. {   XtDestroyApplicationContext(appl_context);
  428.     exit(0);
  429. }
  430.  
  431. /*---------------------------------------------------------------------------+
  432. | ToHex                                                                      |
  433. |         Changes the ascii character values to Hex notation.                  |
  434. +---------------------------------------------------------------------------*/  
  435. static void ToHex (w,ignore1,ignore2)
  436. Widget w; XtPointer ignore1, ignore2;
  437.   int i;
  438.  
  439.     for (i=0; i<COLS; i++)
  440.         XtVaSetValues (value_list[i], XtNlabel, hex_columns[i], NULL);
  441.  
  442. }
  443.  
  444.  
  445. /*---------------------------------------------------------------------------+
  446. | ToDec                                                                      |
  447. |         Changes the ascii character values to Decimal notation.              |
  448. +---------------------------------------------------------------------------*/  
  449. static void ToDec (w,ignore1,ignore2)
  450. Widget w; XtPointer ignore1, ignore2;
  451.   int i;
  452.  
  453.     for (i=0; i<COLS; i++)
  454.         XtVaSetValues (value_list[i], XtNlabel, decimal_columns[i], NULL);
  455.  
  456. }
  457.  
  458.  
  459. /*---------------------------------------------------------------------------+
  460. | ToOct                                                                      |
  461. |         Changest the ascii character values to Octal notation.               |
  462. +---------------------------------------------------------------------------*/  
  463. static void ToOct (w,ignore1,ignore2)
  464. Widget w; XtPointer ignore1, ignore2;
  465.   int i;
  466.  
  467.     for (i=0; i<COLS; i++)
  468.         XtVaSetValues (value_list[i], XtNlabel, octal_columns[i], NULL);
  469.  
  470. }
  471.  
  472.  
  473. /*---------------------------------------------------------------------------+
  474. | Done                                                                       |
  475. |         Pops down the about box popup shell.                                 |
  476. +---------------------------------------------------------------------------*/  
  477. static void Done (w,ignore1,ignore2)
  478. Widget w; XtPointer ignore1, ignore2;
  479.   int i;
  480.  
  481.     XtPopdown (pshell);
  482. }
  483.  
  484.  
  485. /*===========================================================================+
  486. | ACTION FUNCTIONS                                                               | +===========================================================================*/
  487.  
  488. /*---------------------------------------------------------------------------+
  489. | AboutXascii                                                                |
  490. |             Positions and pops up the about box popup shell.                 |
  491. +---------------------------------------------------------------------------*/  
  492. static void AboutXascii (w, event, nuffin1, nuffin2)
  493. Widget            w;
  494. XButtonEvent    event;
  495. String            *nuffin1;
  496. Cardinal        *nuffin2;
  497. {    
  498.     Position    x, 
  499.                 y;
  500.  
  501.     Dimension    width, 
  502.                 height;
  503.  
  504.     int            i;
  505.  
  506.     XtVaGetValues (toplevel,
  507.             XtNwidth, &width,
  508.             XtNheight, &height,
  509.             NULL);
  510.  
  511.     XtTranslateCoords (toplevel,
  512.             (Position) 0,
  513.             (Position) height/2,
  514.             &x, &y,
  515.             NULL);
  516.  
  517.     XtVaSetValues (pshell,
  518.             XtNx, x,
  519.             XtNy, y,
  520.             NULL);
  521.  
  522.     XtPopup (pshell, XtGrabNonexclusive);
  523. }
  524.  
  525. /*===========================================================================+
  526. | UTILITY FUNCTIONS                                                              | +===========================================================================*/
  527.  
  528. /*---------------------------------------------------------------------------+
  529. | Syntax                                                                     |
  530. |        Parses off bad command line options (i.e. the stuff left over when   |
  531. |         XtVaAppInitialize is through).                                       |
  532. +---------------------------------------------------------------------------*/       static void Syntax (argc, argv)
  533. int     argc;
  534. char    **argv;
  535. {
  536.     int     i,
  537.             err = 0;
  538.  
  539.     for (i=1; i < argc; i++)
  540.     {
  541.         if (!err++)
  542.             fprintf (stderr, "\nxascii: unknown command line option\n");
  543.  
  544.         fprintf (stderr, "option:  %s\n", argv[i]);
  545.     }
  546.  
  547. }
  548.  
  549.         
  550.  
  551.         
  552. static char *ascii_val(j, s )
  553. int j;
  554. char *s;
  555. {
  556.     if ( j < ' ' )
  557.         sprintf(s, "^%c", j | 0x40 );
  558.     else if ( j == 127 )
  559.         sprintf(s, "^?");
  560.     else
  561.         sprintf(s, "%c", j );
  562.  
  563.     return s;        
  564. }
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.